package com.cgc.venus.server.job.jobdetail;

import com.cgc.venus.server.core.utils.SpringUtil;
import com.cgc.venus.server.core.utils.StringUtil;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * job基础类
 */
public class AbstractBaseJob implements BaseJob {
    private static Logger logger = LoggerFactory.getLogger(AbstractBaseJob.class);
    private JdbcTemplate jdbcTemplate;
    public AbstractBaseJob() {
        jdbcTemplate= SpringUtil.getBean(JdbcTemplate.class);
    }

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void execute(JobExecutionContext context)
        throws JobExecutionException {
    }

    public void handleEx(String alertMsg,int alertType,int alertStatus,int alertLevel,String ns){
        List objs = new ArrayList<Object>();
        //alert 表新增
        String sql = "INSERT INTO t_job_alarm(id,Alarm_type,Alarm_msg,Alert_level,status,Ns_name,create_time,last_update_time) VALUES (?,?,?,?,?,?,NOW(),NOW())";
        String id = StringUtil.getUUID();
        objs.add(id);
        objs.add(alertType);
        objs.add(alertMsg);
        objs.add(alertLevel);
        objs.add(alertStatus);
        objs.add(ns);
        try {
            jdbcTemplate.update(sql, objs.toArray());
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
    }

    public String getErrorInfoFromException(Exception e) {
        StringWriter sw = null;
        PrintWriter pw = null;
        try {
            sw = new StringWriter();
            pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            return sw.toString();
        } catch (Exception e2) {
            logger.error(e2.getMessage(),e2);
            return "";
        }finally {
            try {
                if(sw!=null){
                    sw.close();
                }
                if(pw!=null){
                    pw.close();
                }
            }catch (Exception e3){
                logger.error(e3.getMessage(),e3);
            }
        }
    }

}  
